import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ConfigManager {
    private static ConfigManager instance;
    private String url;
    private String username;
    private String password;

    private ConfigManager() {
        // 在这里读取数据库配置文件
        try (BufferedReader reader = new BufferedReader(new FileReader("database.properties"))) {
            String line;
            while ((line = reader.readLine())!= null) {
                if (line.startsWith("url=")) {
                    String urlPart = line.substring("url=".length());
                    if (!urlPart.isEmpty()) {
                        url = urlPart;
                    }
                } else if (line.startsWith("username=")) {
                    String usernamePart = line.substring("username=".length());
                    if (!usernamePart.isEmpty()) {
                        username = usernamePart;
                    }
                } else if (line.startsWith("password=")) {
                    String passwordPart = line.substring("password=".length());
                    if (!passwordPart.isEmpty()) {
                        password = passwordPart;
                    }
                }
            }
        } catch (IOException e) {
            System.err.println("读取配置文件时出现错误: " + e.getMessage());
        }
    }

    public static ConfigManager getInstance() {
        if (instance == null) {
            instance = new ConfigManager();
        }
        return instance;
    }

    public String getUrl() {
        return url;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public static void main(String[] args) {
        ConfigManager configManager = ConfigManager.getInstance();
        if (configManager.getUrl()!= null) {
            System.out.println("DB URL: " + configManager.getUrl());
        } else {
            System.out.println("未获取到有效的 URL 配置");
        }
        if (configManager.getUsername()!= null) {
            System.out.println("Username: " + configManager.getUsername());
        } else {
            System.out.println("未获取到有效的用户名配置");
        }
        if (configManager.getPassword()!= null) {
            System.out.println("Password: " + configManager.getPassword());
        } else {
            System.out.println("未获取到有效的密码配置");
        }
    }
}
